package fun.sboy.headacheMemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import fun.sboy.headacheMemo.commons.lang.CONST;
import fun.sboy.headacheMemo.dto.TaskTodoDTO;
import fun.sboy.headacheMemo.entry.TaskTodo;
import fun.sboy.headacheMemo.service.TaskTodoService;
import fun.sboy.headacheMemo.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/taskTodo")
public class TaskTodoController {

    @Autowired
    private TaskTodoService taskTodoService;

    @GetMapping("/{tlId}")
    public ResultUtils getTaskTodoById(@PathVariable("tlId") Integer tlId) {
        TaskTodo taskTodo = taskTodoService.selectTaskTodoById(tlId);
        return ResultUtils.success(taskTodo);
    }

    @GetMapping("/list")
    public ResultUtils getTaskTodoList(Integer startPage) {

        //开启分页插件
        PageHelper.startPage(startPage, CONST.PAGE_SIZE);
        List<TaskTodo> taskTodos = taskTodoService.selectTaskTodos();
        System.out.println("查询到的任务列表：" + taskTodos);
        PageInfo<TaskTodo> taskTodoPageInfo = new PageInfo<>(taskTodos);
        return ResultUtils.success(taskTodoPageInfo);
    }

    @GetMapping("/search")
    public ResultUtils searchTaskTodoList(String searchVal, Integer startPage) {

        //开启分页插件
        PageHelper.startPage(startPage, CONST.PAGE_SIZE);
        List<TaskTodo> taskTodos = taskTodoService.searchTaskTodos(searchVal);
        System.out.println("查询到的任务列表：" + taskTodos);
        PageInfo<TaskTodo> taskTodoPageInfo = new PageInfo<>(taskTodos);
        return ResultUtils.success(taskTodoPageInfo);
    }

    @PostMapping
    public ResultUtils addTaskTodo(@RequestBody TaskTodo taskTodo) {
        System.out.printf("TaskTodo：%s\n", taskTodo);
        taskTodoService.insertTaskTodo(taskTodo);
        return ResultUtils.success(null);
    }

    @PutMapping
    public ResultUtils updateTaskTodo(@RequestBody TaskTodo taskTodo) {
        taskTodoService.updateTaskTodo(taskTodo);
        return ResultUtils.success(null);
    }

    @DeleteMapping("/{tId}")
    public ResultUtils deleteTaskTodo(@PathVariable("tId") String tId) {
        taskTodoService.deleteTaskTodo(tId);
        return ResultUtils.success(null);
    }

}
